<?php


namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CatchAllOptionsRequestsProvider extends ServiceProvider
{
    public function register()
    {
        $request = app('request');

        if ($request->isMethod('OPTIONS')) {
            app()->options($request->path(), function () {
                $headers = [
                    'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE',
                    'Access-Control-Allow-Headers' => 'Origin,Accept,x-requested-with,content-type,Authorization,xsrfCookieName',
                    'Access-Control-Allow-Credentials' => 'true',
                    'Access-Control-Max-Age' => 1728000
                ];
                $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
                $headers['Access-Control-Allow-Origin'] = $origin;
                foreach ($headers as $name => $value) {
                    header($name . ': ' . $value);
                }
                return response('', 200);
            });
        }
    }
}

